package com.ysu.his.admin.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.ysu.his.admin.mapper.DepartmentUserMapper;
import com.ysu.his.admin.mapper.SchedulingMapper;
import com.ysu.his.admin.mapper.UserMapper;
import com.ysu.his.admin.service.SchedulingService;
import com.ysu.his.admin.websocket.MyWebSocketHandler;
import com.ysu.his.dto.DoctorScheduleDto;
import com.ysu.his.entity.schedule.DoctorSchedule;
import com.ysu.his.entity.user.Department;
import com.ysu.his.entity.user.User;
import com.ysu.his.exception.HISException;
import com.ysu.his.vo.DoctorScheduleVo;
import com.ysu.his.vo.common.ResultCodeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

@Service
public class SchedulingServiceImpl implements SchedulingService
{

    @Autowired
    private SchedulingMapper schedulingMapper;

    @Autowired
    private DepartmentUserMapper departmentUserMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MyWebSocketHandler myWebSocketHandler;

    private void sendMessageToDoctor(DoctorSchedule doctorSchedule, String message)
    {
        try
        {
            myWebSocketHandler.sendMessageToUser(String.valueOf(doctorSchedule.getDoctorId()),
                    doctorSchedule.getDoctorName() + message);
        } catch (IOException e)
        {
            throw new RuntimeException(e);
        }
    }


    @Override
    public void cancelSchedule(Long id)
    {
        schedulingMapper.cancelSchedule(id);
    }

    @Override
    public DoctorScheduleVo getSchedulingByDoctorId(Long doctorId, String scheduleDate)
    {
        Date date;
        try
        {
            SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd");
            date = originalFormat.parse(scheduleDate);
        } catch (ParseException e)
        {
            throw new RuntimeException(e);
        }

        DoctorSchedule doctorSchedule = schedulingMapper.getSchedulingByDoctorId(doctorId, date);

        if (doctorSchedule==null)
        {
            return new DoctorScheduleVo();
        }

        DoctorScheduleVo doctorScheduleVo = new DoctorScheduleVo();
        BeanUtils.copyProperties(doctorSchedule, doctorScheduleVo);


        String schedulePeriods = doctorSchedule.getSchedulePeriod();
        doctorScheduleVo.setSchedulePeriods(Arrays.asList(schedulePeriods.split(",")));
        return doctorScheduleVo;
    }

    @Override
    @Transactional
    public void addOrUpdateScheduling(DoctorScheduleDto doctorScheduleDto)
    {
        Long doctorId = doctorScheduleDto.getDoctorId();
        User userInfoById = userMapper.getUserInfoById(doctorId);


        Department department = departmentUserMapper.getDepartmentByDoctorId(doctorId);

        Date scheduleDate = doctorScheduleDto.getScheduleDate();
        DoctorSchedule scheduling = schedulingMapper.getSchedulingByDoctorId(doctorId, scheduleDate);

        DoctorSchedule doctorSchedule = new DoctorSchedule();
        BeanUtils.copyProperties(doctorScheduleDto,doctorSchedule);

        doctorSchedule.setDoctorName(userInfoById.getUserName());
        doctorSchedule.setDepartmentName(department.getDepartmentName());
        doctorSchedule.setDepartmentId(department.getId());

        if (CollectionUtil.isEmpty(doctorScheduleDto.getSchedulePeriods()))
        {
            schedulingMapper.cancelSchedule(doctorId);
            return;
        }

        if (scheduling == null)
        {

            doctorSchedule.setSchedulePeriod(String.join(",",doctorScheduleDto.getSchedulePeriods()));

            schedulingMapper.addSchedulingToDoctor(doctorSchedule);
        } else
        {
            schedulingMapper.cancelSchedule(doctorId);
            doctorSchedule.setSchedulePeriod(String.join(",",doctorScheduleDto.getSchedulePeriods()));
            schedulingMapper.addSchedulingToDoctor(doctorSchedule);
        }

        sendMessageToDoctor(doctorSchedule, "您有新的排班消息");
    }
}
